package com.houzehui.controller;

import com.houzehui.entity.UserFund;
import com.houzehui.entity.Users;
import com.houzehui.service.UsersService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class UsersController {

    @Resource
    RedisTemplate redisTemplate;

    @DubboReference
    private UsersService usersService;

    @RequestMapping("registerto")
    public String registerto(){
        return "register";
    }

    @RequestMapping("register")
    public String register(Users users,Integer yzm){
        String phone=users.getPhone();
        String re_code = (String) redisTemplate.opsForValue().get(phone);
        if(re_code.equals("login"+yzm)){
            int insertusers = usersService.insertusers(users);
            return "login";
        }else{

            return "registerto";
        }


    }

    @RequestMapping("getyzm")
    @ResponseBody
    public Integer getyzm(String phone){
          int sj = (int)(Math.random()*8998)+1000+1;
        System.err.println(sj);
        redisTemplate.opsForValue().setIfAbsent(phone,"login"+sj, 60, TimeUnit.SECONDS);
        return sj;

    }



    @RequestMapping("login")
    public String login(@Validated @ModelAttribute("users") Users users, BindingResult result, HttpServletRequest request){
        if(result.hasErrors()){
            return "login";
        }else{

            Users users1 = usersService.list(users);
            if(users1==null){
                result.rejectValue("password","xxoo","用户名或密码不对");
                return "login";
            }
            HttpSession session = request.getSession();
            session.setAttribute("user",users1);
            return "redirect:list";
        }

    }

@RequestMapping("grzx")
    public String grzx(HttpServletRequest request){
    Users  user = (Users) request.getSession().getAttribute("user");

    List<UserFund> selelist = usersService.selelist(user.getId());
    System.out.println(selelist);
    request.setAttribute("fundd",selelist);
    Double selezprice = usersService.selezprice(user.getId());
    request.setAttribute("zp",selezprice);
    return "glist";
}

@RequestMapping("xh")
    public  String xh(HttpSession session){
        session.invalidate();
        return "loginto";
}

@RequestMapping("loginto")
    public  String loginto(Users users){
        return "login";
}

}
